package cn.bdqn.pn.service.base.impl;

import cn.bdqn.pn.pojo.base.City;
import cn.bdqn.pn.reposity.base.CityRepository;
import cn.bdqn.pn.service.base.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service("cityService")
public class CityServiceImpl implements CityService {
    @Autowired
    private CityRepository cityRepository;

    @Override
    public City addCity(City city) throws Exception {
        return cityRepository.save(city);
    }

    /**
     * 查询省份/城市 不分页
     * @param city
     * @return
     */
    @Override
    public List<City> listCity(City city) throws Exception {
        Specification<City> specification=new Specification<City>() {
            @Override
            public Predicate toPredicate(Root<City> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list=new ArrayList<>();
                Predicate[] predicate =new Predicate[list.size()];
                list.add(cb.equal(root.get("fatherId"),city.getFatherId()));
                return cb.and(list.toArray(predicate));
            }
        };
        return cityRepository.findAll(specification);
    }

    /**
     * 分页展示数据
     * @param city 城市对象
     * @param pageNo 当前页数（非下标）
     * @param pageSize 页大小（默认为10）
     * @return 分页数据
     */
    @Override
    public Page<City> listCity(City city, Integer pageNo, Integer pageSize) throws Exception {
        Specification<City> specification=new Specification<City>() {
            @Override
            public Predicate toPredicate(Root<City> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list=new ArrayList<>();
                Predicate[] predicate =new Predicate[list.size()];
                list.add(cb.equal(root.get("fatherId"),city.getFatherId()));
                System.out.println("查询城市并分页展示数据");
                return cb.and(list.toArray(predicate));
            }
        };
        Pageable pageable=new PageRequest(pageNo-1,pageSize==0?10:pageSize);
        return cityRepository.findAll(specification,pageable);
    }

    /**
     * 检查重复
     * @param city 城市对象（主需城市名称）
     * @return
     */
    @Override
    public City checkRepeat(City city) throws Exception {
        Specification<City> specification=new Specification<City>() {
            @Override
            public Predicate toPredicate(Root<City> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list=new ArrayList<>();
                Predicate[] predicate =new Predicate[list.size()];
                list.add(cb.equal(root.get("cityName"),city.getCityName()));
                System.out.println("检查重复");
                return cb.and(list.toArray(predicate));
            }
        };
        return cityRepository.findOne(specification);
    }
    /**
     * 根据id查找
     * @param city 城市对象（主需城市id）
     * @return
     * @throws Exception
     */
    @Override
    public City findCity(City city) throws Exception {
        Specification<City> specification=new Specification<City>() {
            @Override
            public Predicate toPredicate(Root<City> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list=new ArrayList<>();
                Predicate[] predicate =new Predicate[list.size()];
                list.add(cb.equal(root.get("cityId"),city.getCityId()));
                return cb.and(list.toArray(predicate));
            }
        };
        return cityRepository.findOne(specification);
    }

    /**
     * 查询城市总数
     * @return
     */
    @Override
    public Long cityCount() throws Exception {
        return cityRepository.count();
    }

    @Override
    public City findCityBycityId(Integer cityId) throws Exception {
        return cityRepository.findCityByCityId(cityId);
    }

    @Override
    public City getCityByid(Integer id) {
        return cityRepository.findOne(id);
    }

    /**
     * 删除城市
     * @param cityId
     */
    @Override
    public void delCity(Integer cityId)  throws Exception{
        cityRepository.delete(cityId);
    }

    /**
     * 修改信息
     * @param city
     * @return
     * @throws SQLException
     */
    @Override
    public boolean updateCity(City city) throws Exception {
        if(cityRepository.exists(city.getCityId())){
            cityRepository.saveAndFlush(city);
            return true;
        }
        return false;
    }

    /**
     * 查看全部的城市
     * @return
     * @throws SQLException
     */
    @Override
    public List<City> getAllListCityInfo() throws SQLException {
        return cityRepository.findAll();
    }

}
